<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin_member extends MY_Controller {

	function Admin_member()
	{
		parent::MY_Controller();
        parent::check_admin_login();
	}
	
	function member_list($type='CM',$offset=0){
		$this->load->model('Mmember');
		
		$total = $this->Mmember->get_member_count($type);

		$this->load->library('pagination');

		$config['base_url'] = '/admin_member/member_list/'.$type.'/';
		$config['total_rows'] = $total;
		$config['per_page'] = '11';
		$config['uri_segment'] = 4;
		$config['num_links'] = 3;
/*		$config['full_tag_open'] = '<ul id="pagebar">';
		$config['full_tag_close'] = '</ul>';
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		$config['cur_tag_open'] = '<li><a href="#">';
		$config['cur_tag_close'] = '</a></li>';
		$config['first_link'] = '&#171;';
		$config['first_tag_open'] = '<li>';
		$config['first_tag_close'] = '</li>';
		$config['last_link'] = '&#187;';
		$config['last_tag_open'] = '<li>';
		$config['last_tag_close'] = '</li>';
		$config['next_link'] = '&gt;';
		$config['next_tag_open'] = '<li>';
		$config['next_tag_close'] = '</li>';
		$config['prev_link'] = '&lt;';
		$config['prev_tag_open'] = '<li>';
		$config['prev_tag_close'] = '</li>';*/

		$this->pagination->initialize($config);

		$page_html = $this->pagination->create_links();
		
		$limit = $config['per_page'];
		
		$members = $this->Mmember->get_member_list($type,$offset,$limit);
		$this->load->model('member_type');
        $types = $this->member_type->get_all_types();
		$this->template->view('admin/member_list',array('members'=>$members,'types'=>$types,'page_html'=>$page_html));
	}
	
	function search($offset=0){
		$s_type 	= parent::get_session_value('utype');
		$s_method 	= parent::get_session_value('method');
		$s_word 	= parent::get_session_value('word');
		$utype 	= $this->input->post('utype') ? $this->input->post('utype') : $s_type;
		$method = $this->input->post('method') ? $this->input->post('method') : $s_method;
		$word 	= $this->input->post('word') ? $this->input->post('word') : $s_word;
		parent::set_session_value('utype',$utype);
		parent::set_session_value('method',$method);
		parent::set_session_value('word',$word);
		
		$this->load->model('Mmember');
		$total = $this->Mmember->get_search_count($utype,$method,$word);

		$this->load->library('pagination');

		$config['base_url'] = '/admin_member/search/';
		$config['total_rows'] = $total;
		$config['per_page'] = '11';
		$config['uri_segment'] = 3;
		$config['num_links'] = 3;

		$this->pagination->initialize($config);

		$page_html = $this->pagination->create_links();
		
		$limit = $config['per_page'];
		
		$this->load->model('member_type');
        $types = $this->member_type->get_all_types();
		
		$members = $this->Mmember->get_search_result($utype,$method,$word,$offset,$limit);
		$this->template->view('admin/member_list',array('members'=>$members,'types'=>$types,'page_html'=>$page_html));
	}
	
	function member_new(){
		$this->load->model('member_type');
        $types = $this->member_type->get_all_types();
		$this->template->view('admin/member_new',array('types'=>$types));
	}

    function member_view($uid){
        $this->load->model('Mmember');
        $this->load->model('Morders');
        $info = $this->Mmember->get_member_info($uid);
        $temp = $this->Morders->get_member_order($info['username']);
        $orders = array();
        if($temp){
            foreach($temp as $arr){
                $order_info = $this->_get_order_info($arr['order_id']);
                $orders[] = $order_info;
            }
        }
        $this->template->view('admin/member_view',array('info'=>$info,'orders'=>$orders));
    }

    function member_edit($uid){
        $this->load->model('Mmember');
        $info = $this->Mmember->get_member_info($uid);
        $this->template->view('admin/member_edit',array('info'=>$info));
    }

    function member_modify(){
    	$uid  = $this->input->post('uid');
        $form = array('username','lvl','gender','realname','qq','email','msn','tel','bank_account','discount','pwd','pwd2');
        $data = $this->input->get_data($form);
        //print_r($data);exit;
        if (!$data['pwd'] || !$data['pwd2']){
        	unset($data['pwd']);
        	unset($data['pwd2']);
        } else if ($data['pwd'] && $data['pwd2']){
        	if ($data['pwd'] != $data['pwd2']){
        		parent::redirect('两次密码输入不一致，请返回重新填写！','back',3);
        	} else {
        		$data['password'] = $data['pwd'];
        		unset($data['pwd']);
        		unset($data['pwd2']);
        	}
        }
        $this->load->model('Mmember');
        if ($this->Mmember->update_member_data($uid,$data)){
        	echo '修改成功！';
        } else {
        	echo '修改失败！';
        }
    }
    
    function member_del($uid){
    	$this->load->model('Mmember');
    	$data = array('state'=>-1);
    	if ($this->Mmember->update_member_data($uid,$data)){
        	echo '删除成功！';
        } else {
        	echo '删除失败！';
        }
    }

	
	function member_add_deal(){
		$utype = $this->input->post('utype');
		$count = $this->input->post('count');
		if (!$utype || !$count) {
			echo '你没有选择用户类型或数量，请返回重新选择！';
			return;
		}
		$this->load->model('Mmember');
		$discount_info = $this->Mmember->get_discount_info($utype);
		$ids = $this->Mmember->get_valid_member_id($count);
		$default_pwd = $this->config->item('default_member_pwd');
		if ($this->Mmember->deal_add_member($utype,$ids,$default_pwd,$discount_info)) {
			if ($this->Mmember->update_member_id_state($ids)) {
				echo '用户生成成功！';
			} else {
				echo '用户生成失败！';
			}
		} else {
			echo 'deal_add_member fail!';
		}
	}
	
	function member_id_new($count=10000){
		$this->load->model('mmember');
		if($this->mmember->new_member_ids($count)){
			echo '成功生成 '.$count.' 个帐号！';
		} else {
			echo '帐号生成失败！';
		}
	}

    function type_list() {
        $this->load->model('member_type');
        $types = $this->member_type->get_all_types();
        //print_r($types);exit;
        $this->template->view('admin/member_type_list',array('types'=>$types));
    }

    function type_edit($id='') {
        if($id === '') {
            return;
        }
        $this->load->model('member_type');
        $info = $this->member_type->get_type_info($id);
        $this->template->view('admin/member_type_edit',array('info'=>$info));
    }

    function type_new() {
        $this->template->view('admin/member_type_add');
    }

    function type_add() {
        $values = array('en_name'=>$this->input->post('en_name'),'cn_name'=>$this->input->post('en_name'));
        $this->load->model('member_type');
        if($this->member_type->insert_type($values)) {
            echo '成功插入新的用户类型';
        } else {
            echo '插入失败！';
        }
    }

    function type_modify() {
        $id = $this->input->post('id');
        $en_name = $this->input->post('en_name');
        $cn_name = $this->input->post('cn_name');
        $discount = $this->input->post('discount');
        $array = array('id'=>$id,'en_name'=>$en_name,'cn_name'=>$cn_name,'discount'=>$discount);
        $this->load->model('member_type');
        if($this->member_type->update_type_info($array)) {
            echo '更新成功！';
        } else {
            echo '更新失败！';
        }
    }

    function type_del($id='') {
        if($id === '') {
            echo 'id is null';
            return;
        }
        $this->load->model('member_type');
        if($this->member_type->delete_type($id)) {
            echo '删除成功！';
        } else {
            echo '删除失败！';
        }
    }

    function _get_order_info($order_id) {
        $this->load->model('Morders');
        $info = $this->Morders->get_order_info($order_id);
        $this->load->model('Mproduct');
        $p_info = $this->Mproduct->get_product_info($info['product']);
        $info['p_name'] = $p_info['p_name'];
        $info['p_days'] = $p_info['p_days'];
        $this->load->model('Mgames');
        $info['game_name']      = $this->Mgames->get_game_name($info['game_id']);
        $info['area_name']      = $this->Mgames->get_area_name($info['game_area']);
        $info['server_name']    = $this->Mgames->get_server_name($info['game_server']);
        $info['group_name']     = $this->Mgames->get_group_info('en_name',$info['role_group'],'cn_name');
        $info['career_name']    = $this->Mgames->get_career_info('en_name',$info['role_career'],'cn_name');
        $this->config->load('setup');
        $states = $this->config->item('order_state');
        $info['cn_state'] = $states[$info['order_state']];
        return $info;
    }
}
?>